//
//  ChangeIphoneAndPassWordViewController.m
//  Router
//
//  Created by Zhen Huang on 2018/7/4.
//  Copyright © 2018年 James. All rights reserved.
//

#import "ChangeIphoneAndPassWordViewController.h"
#define ALPHANUM @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-"//输入用户名只允许英文、数字、下划线或减号

@interface ChangeIphoneAndPassWordViewController ()
<UITextFieldDelegate>
@property (nonatomic, strong)UITextField *OldPassWordText;
@property (nonatomic, strong)UITextField *NewPassWordText;
@property (nonatomic, strong)UITextField *MakeSurePassText;
@property (nonatomic, strong)UIButton *SureBtn;

@end

@implementation ChangeIphoneAndPassWordViewController
- (instancetype)initWithType:(NSString *)type {
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackGroundColor;
    [self.view addSubview:self.OldPassWordText];
    [self.view addSubview:self.NewPassWordText];
    [self.view addSubview:self.MakeSurePassText];
    [self createUserCodeAndPassWord];
    [self.view addSubview:self.SureBtn];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(UITextFieldTextDidChangeEditingNotification:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    // Do any additional setup after loading the view.
}

- (void)UITextFieldTextDidChangeEditingNotification:(NSNotification *)noti {
    UITextField *textField = noti.object;
    if (textField.tag == 50) {
        self.OldPassWordText.text = textField.text;
    }else if (textField.tag == 51) {
        self.NewPassWordText.text = textField.text;
    }else if (textField.tag == 52) {
        self.MakeSurePassText.text = textField.text;
    }
}
- (UITextField *)OldPassWordText {
    if (!_OldPassWordText) {
        _OldPassWordText = [[UITextField alloc]initWithFrame:CGRectMake(20, ScreenHeight / 2 - 56 - 15.5 - 25, ScreenWidth - 200, 25)];
        _OldPassWordText.delegate = self;
        _OldPassWordText.placeholder = @"请输入旧密码";
        _OldPassWordText.font = [UIFont fontWithName:PingFangSCRegular size:15];
        _OldPassWordText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_OldPassWordText.placeholder attributes:@{NSForegroundColorAttributeName: [Networking colorWithHexString:@"#455A6A"]}];
        _OldPassWordText.textColor = kTitleBackGroundColor;
        _OldPassWordText.tag = 50;
    }
    return _OldPassWordText;
}
- (UITextField *)NewPassWordText {
    if (!_NewPassWordText) {
        _NewPassWordText = [[UITextField alloc]initWithFrame:CGRectMake(20, ScreenHeight / 2  - 15.5 - 25, ScreenWidth - 200, 25)];
        _NewPassWordText.delegate = self;
        _NewPassWordText.placeholder = @"请输入新密码";
        _NewPassWordText.font = [UIFont fontWithName:PingFangSCRegular size:15];
        _NewPassWordText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_NewPassWordText.placeholder attributes:@{NSForegroundColorAttributeName: [Networking colorWithHexString:@"#455A6A"]}];
        _NewPassWordText.textColor = kTitleBackGroundColor;
        _NewPassWordText.tag = 51;
    }
    return _NewPassWordText;
}
- (UITextField *)MakeSurePassText {
    if (!_MakeSurePassText) {
        _MakeSurePassText = [[UITextField alloc]initWithFrame:CGRectMake(20, ScreenHeight / 2  + 56 - 15.5 - 25, ScreenWidth - 200, 30)];
        _MakeSurePassText.delegate = self;
        _MakeSurePassText.placeholder = @"确认密码";
        _MakeSurePassText.font = [UIFont fontWithName:PingFangSCRegular size:15];
        _MakeSurePassText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_MakeSurePassText.placeholder attributes:@{NSForegroundColorAttributeName: [Networking colorWithHexString:@"#455A6A"]}];
        _MakeSurePassText.textColor = kTitleBackGroundColor;
        _MakeSurePassText.tag = 52;
    }
    return _MakeSurePassText;
}
- (void)createUserCodeAndPassWord {
    for (int i = 0; i < 3; i ++) {
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(20, ScreenHeight / 2 - 56 + 56 *i, ScreenWidth - 40, 1)];
        lineView.backgroundColor = [Networking colorWithHexString:@"#455A6A"];
        [self.view addSubview:lineView];
    }
}
- (UIButton *)SureBtn {
    if (!_SureBtn) {
        _SureBtn = [[UIButton alloc]initWithFrame:CGRectMake(28,ScreenHeight - 150,ScreenWidth - 56,55)];
        _SureBtn.layer.masksToBounds = YES;
        _SureBtn.layer.cornerRadius = 25;
        if (iPhone5) {
            _SureBtn.height = 40;
            _SureBtn.layer.cornerRadius = 20;
        }
        _SureBtn.titleLabel.font = [UIFont fontWithName:PingFangSCRegular size:16];
        _SureBtn.backgroundColor = kNavBlueColor;
        [_SureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_SureBtn setTitle:@"确认修改" forState:UIControlStateNormal];
        [[_SureBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
            
            NSString *url = F(@"%@/users/change_password", BaseUrl);
            NSDictionary *paramDic = @{@"old_password":self.OldPassWordText.text?:@"",
                                       @"password":self.NewPassWordText.text?:@"",
                                       @"password2":self.MakeSurePassText.text?:@""
                                       };
            NSString *param = [self dictionaryToJson:paramDic];
            NSString *Token = F(@"Token %@", [[TMCache sharedCache] objectForKey:@"Token"]);
            [self receivePOSTWithTokenURLString:url method:@"POST" body:param Token:Token Blocks:^(NSDictionary *tempData, NSInteger state) {
                if (tempData) {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            }];
        }];
    }
    return _SureBtn;
}
//点击界面的任意地方，回收键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
//回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
